package com.zzh.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ReplacementSpan;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;


public class TextAgainstView extends AppCompatTextView {

    private int mAgainstLength = 5; // 最大文字长度
    private int mAgainstMinInterval = 2; // 最小间隔

    public TextAgainstView(Context context) {
        this(context, null);
    }

    public TextAgainstView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextAgainstView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextAgainstView);
        if (typedArray != null) {
            mAgainstLength = typedArray.getInteger(R.styleable.TextAgainstView_againstLength, 5);
            mAgainstMinInterval = typedArray.getDimensionPixelSize(R.styleable.TextAgainstView_againstMinInterval, 2);
            typedArray.recycle();
        }
        setText2(getText());
    }

    public void setText2(CharSequence text, int againstLength) {
        this.mAgainstLength = againstLength;
        setText2(text);
    }

    public void setText2(CharSequence text, int againstLength,int againstMinInterval) {
        this.mAgainstLength = againstLength;
        this.mAgainstMinInterval = againstMinInterval;
        setText2(text);
    }

    public void setText2(CharSequence text) {
        if (TextUtils.isEmpty(text)) {
            super.setText("", BufferType.NORMAL);
            return;
        }
        int length = text.length();
        StringBuilder builder = new StringBuilder();
        int updateCount = 0;
        for (int i = 0; i < length; i++) {
            updateCount++;
            builder.append(text.charAt(i));
            if (updateCount == mAgainstLength && i != length - 1) {
                updateCount = 0;
                builder.append("\n");
            }
        }

        SpannableString spannableString = new SpannableString(builder);

        spannableString.setSpan(new CustomSpacingSpan(), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        super.setText(spannableString, BufferType.SPANNABLE);
    }

    class CustomSpacingSpan extends ReplacementSpan {
        private int allWidth;
        private float singleWidth;

        public CustomSpacingSpan() {
        }

        @Override
        public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
            singleWidth = paint.measureText("汉", 0, 1); // 此处计算以汉字大小为基础
            allWidth = (int) (singleWidth * mAgainstLength + mAgainstMinInterval * (mAgainstLength - 1));
            return allWidth;
        }

        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
            float dx = x;
            float interval = mAgainstMinInterval;
            int length = end - start;
            if (length < mAgainstLength && length != 1) {
                interval = (allWidth - (singleWidth * length)) / (length - 1);
            }
            for (int i = start; i < end; i++) {
                float nowLength = paint.measureText(text, i, i + 1);
                float startX = dx;
                if (i == start) {
                    // 不做处理
                } else if (i == end) {
                    startX += singleWidth - nowLength;
                } else {
                    startX += (singleWidth - nowLength) / 2;
                }
                canvas.drawText(text, i, i + 1, startX, y, paint);
                dx += singleWidth + interval;
            }
        }
    }
}
